package com.tanhua.server.controller;

import com.tanhua.commons.utils.UserHolder;
import com.tanhua.domain.UserInfo;
import com.tanhua.server.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 保存用户信息接口
     * @param userInfo
     * @param token
     * @return
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo
            , @RequestHeader("Authorization") String token){
        //向userInfo中设置入id
        userInfo.setId(UserHolder.getUserId());
        //为用户设置信息
        userInfoService.save(userInfo);
        return ResponseEntity.ok(null);
    }

    @PostMapping("/loginReginfo/head")
    public ResponseEntity head(MultipartFile headPhoto
            , @RequestHeader("Authorization") String token) throws IOException {

        Long userId = UserHolder.getUserId();
        //为用户设置信息
        userInfoService.updateHead(headPhoto,userId);
        return ResponseEntity.ok(null);
    }



}
